package design.simdi.code.final_paper.site.panels.sesiuni;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import design.simdi.code.final_paper.core.daos.SesiuneDao;
import design.simdi.code.final_paper.core.entities.Sesiune;
import design.simdi.code.final_paper.site.application.WicketApplication;
import design.simdi.code.final_paper.site.panels.PagePanel;
import design.simdi.code.final_paper.utils.ActionToolbar;
import design.simdi.code.final_paper.utils.MyDataTable;

/**
 * 
 * @author Marius Dumitriu - (Jun 24, 2010 - 12:33:07 AM)
 * 
 */
public class InscriereTabPanel extends PagePanel {

	private static final long serialVersionUID = 1L;

	@SpringBean
	private SesiuneDao sesiuneDao;
	private SimpleDateFormat format;

	public InscriereTabPanel(String id) {
		super(id);
		setOutputMarkupId(true);
		InjectorHolder.getInjector().inject(this);
		((WicketApplication) getApplication()).setShowErrors(true);

	}

	@Override
	public void panelInit() {
		format = new SimpleDateFormat("dd/MM/yyyy");
		List<IColumn<Sesiune>> columns = new ArrayList<IColumn<Sesiune>>();

		columns.add(new PropertyColumn<Sesiune>(new ResourceModel("name"), "name", "name"));
		columns.add(new AbstractColumn<Sesiune>(new ResourceModel("data-start"), "dataStart") {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<Sesiune>> cellItem, String componentId,
					IModel<Sesiune> rowModel) {
				Sesiune s = rowModel.getObject();
				cellItem.add(new Label(componentId, format.format(s.getDataStart())));
			}

		});
		columns.add(new AbstractColumn<Sesiune>(new ResourceModel("data-stop"), "dataStop") {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<Sesiune>> cellItem, String componentId,
					IModel<Sesiune> rowModel) {
				Sesiune s = rowModel.getObject();
				cellItem.add(new Label(componentId, format.format(s.getDataStop())));
			}

		});
		columns.add(new PropertyColumn<Sesiune>(new ResourceModel("description"), "description"));
		columns.add(new AbstractColumn<Sesiune>(new ResourceModel("selected"), "defaulta") {

			private static final long serialVersionUID = 1L;

			public void populateItem(Item<ICellPopulator<Sesiune>> cellItem, String componentId,
					IModel<Sesiune> rowModel) {
				Sesiune s = rowModel.getObject();
				if (s.isDefaulta())
					cellItem.add(new Label(componentId, new ResourceModel("yes")));
				else
					cellItem.add(new Label(componentId, new ResourceModel("no")));
			}
		});

		SortableSesiuneDataProvider sortableSesiuneDataProvider = new SortableSesiuneDataProvider();

		add(new MyDataTable<Sesiune>("table", columns, sortableSesiuneDataProvider, getRowsPerPage()) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public ActionToolbar getActionToolbar() {
				return new SesiuneActionToolbar(this);
			}
		});

	}

	public void setSesiuneDao(SesiuneDao sesiuneDao) {
		this.sesiuneDao = sesiuneDao;
	}

	public SesiuneDao getSesiuneDao() {
		return sesiuneDao;
	}
}